
let routerData = [
    {
        name: "首页",
        path: "/0",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "协会简介",
        path: "/1",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "协会公告",
        path: "/2",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "近期活动",
        path: "/3",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "理事单位",
        path: "/4",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "会员风采",
        path: "/5",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "在线投稿",
        path: "/6",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    },
    {
        name: "下属二级机构",
        path: "/7",
        icon: "el-icon-s-home",
        cssUrl: "",
        children: []
    }
]

// 路由操作记录
let navaState = {
    "before": {
        path: "/home",
        nav: {},
        router: {
            name: "首页",
            path: "/",
            icon: "el-icon-s-home",
            cssUrl: "./css/home.css",
            children: []
        }
    },
    "after": {
        path: "/home",
        nav: {},
        router: {
            name: "首页",
            path: "/",
            icon: "el-icon-s-home",
            cssUrl: "./css/home.css",
            children: []
        }
    }
}


let routerCallbacks = [];

let liList = document.getElementsByClassName("li_item");
let navPer = document.getElementsByClassName('nav_list_box')[0];
let moNavBtn = document.getElementsByClassName('menu_btn')[0];
// 事件代理：在父元素上添加事件监听器
navPer.addEventListener('click', function (event) {
    event.stopPropagation();
    let target = event.target;
    let navPath = target.getAttribute("path");

    console.log(target)
    if (target.tagName == "li" || target.tagName == "LI") {
        // 如果是移动端则执行移动端菜单按钮点击事件
        let moBtnStyles = window.getComputedStyle(moNavBtn);
        if (moBtnStyles.getPropertyValue("display") == "block") {
            moNavBtnClick();
        }
        if (navaState.after.path == navPath) {
            console.log(navPath + "  为当前页面无需切换。。。。。。。")
            return;
        }

        navCheck(navPath);
    }

});

// 菜单点击处理事件
function navCheck(path) {
    let target = {};
    // 如果点击的是菜单 则更新菜单状态
    for (let index = 0; index < liList.length; index++) {
        const element = liList[index];
        if (element.getAttribute("path") == path) {
            element.classList.add('sel');
            target = element;
        } else {
            element.classList.remove('sel');
        }
    }

    // 如果点击的是路由 则更新路由状态
    if (target.tagName == "li" || target.tagName == "LI") {
        console.log("点击路由：" + path)
        navaStateUpdate(path, target)
        console.log("头部路由切换完成" + JSON.stringify(navaState))
        
        if (routerCallbacks.length > 0) {
            console.log("头部路由切换完成----------执行回调path:" + navaState.after.path + "         路由：" + JSON.stringify(navaState.after.router))
            for (let index = 0; index < routerCallbacks.length; index++) {
                const element = routerCallbacks[index];
                if (element) {
                    element(navaState.after.path, navaState.after.router)
                }
            }
        }
    }
}

moNavBtn.addEventListener('click', function (event) {
    event.stopPropagation();
    let target = event.target;
    moNavBtnClick();

});

function moNavBtnClick() {
    if (navPer.getAttribute("class").indexOf("moNavBtnHide") < 0) {
        navPer.classList.add('moNavBtnHide');

    } else {
        navPer.classList.remove('moNavBtnHide');
    }
}





// 路由状态更新
function navaStateUpdate(path, target) {
    
    if (navaState.after.path == path) {
        locationActivity("check", {
            path: path,
            title: navaState.after.router.name
        });
        return
    } else {
        console.log("之前路由：" + navaState.before.path)
        console.log("当前路由：" + navaState.after.path)
        navaState.before = navaState.after;
        navaState.after.path = path;
        navaState.after.nav = target;
        navaState.after.router = getRouter(path);
    }
    locationActivity("check", {
        path: path,
        title: navaState.after.router.name
    });
   
}

//  获取路由
function getRouter(path) {
    let router = {};

    for (let i = 0; i < routerData.length; i++) {
        let item = routerData[i];
        if (item.path == path) {
            router = item;
            break;
        } else {
            if (item.children.length > 0) {
                for (let j = 0; j < item.children.length; j++) {
                    let child = item.children[j];
                    if (child.path == path) {
                        router = child;
                        break;
                    }
                }
            }
        }
    }
    return router;
}


function router(callback) {
    // console.log(callback)

    if (routerCallbacks.length == 0) {
        routerCallbacks.push(callback);
    } else {
        for (let index = 0; index < routerCallbacks.length; index++) {
            const element = routerCallbacks[index];
            if (element == callback) {
                return;
            } else {
                routerCallbacks.push(callback);
            }

        }
    }
}




